package com.demo.flowable;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.FormService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.form.api.FormInfo;
import org.flowable.task.api.Task;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 出行方式
 * @author K
 */
@Slf4j
@Component
public class FlowableTripService {

    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource(name = "formServiceBean")
    private FormService formService;

    public void trip(Map<String,String> vars) {
        //流程定义的key,对应请假的流程图
        String processDefinitionKey = "trip";
        //业务代码，根据自己的业务用
        String businessKey = "trip";
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey);
        log.info("启动成功:{}", processInstance.getId());

        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        // 申领任务
        taskService.claim(task.getId(), "my");

        FormInfo taskFormModel = taskService.getTaskFormModel(task.getId());
        log.info("form id : {}，task id : {}", taskFormModel.getId(), task.getId());
        // 提交表单，等同于调用taskService.complete
        formService.submitTaskFormData(task.getId(), vars);
    }
}
